package com.shardingsphere.example.controller;

import com.shardingsphere.example.entity.User;
import com.shardingsphere.example.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;


    /**
     * 添加用户
     */
    @PostMapping
    public String addUser(@RequestBody User user) {
        boolean result = userService.save(user);
        return result ? "添加成功" : "添加失败";
    }

    /**
     * 根据userId查询用户
     */
    @GetMapping("/{userId}")
    public User getUserByUserId(@PathVariable String userId) {
        return userService.getUserByUserId(userId);
    }

    /**
     * 根据年龄查询用户
     */
    @GetMapping("/age/{age}")
    public List<User> getUsersByAge(@PathVariable Integer age) {
        return userService.getUsersByAge(age);
    }

    /**
     * 获取所有用户
     */
    @GetMapping
    public List<User> getAllUsers() {
        return userService.getAllUsers();
    }
}

